﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using BoYuan.Framework.Uitility;
using BoYuan.Framework.Uitility.Extensions;

namespace BoYuan.DBServices
{
    public class SysLoginLog
    {
        /// <summary>
        /// 添加登录日志
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <param name="loginState"></param>
        /// <param name="remark"></param>
        /// <param name="userid"></param>
        public static bool AddLoginLog(string username, string pwd, EnumLib.SysLoginLog.LoginState loginState, string remark = "", int userid = 0)
        {
            //如果不需要登录日志功能，请
            //return false;

            Entity.SysLoginLog log = new Entity.SysLoginLog();
            log.UserName = username.ToMaxLengthString(100);
            log.LoginState = (byte) loginState;
            if (loginState != EnumLib.SysLoginLog.LoginState.登录成功)
            {
                log.PwdShow = pwd.ToMaxLengthString(100);
                log.userId = 0;
            }
            else
            {
                log.PwdShow = string.Empty;
                log.userId = userid;
            }
            log.Addtime = DateTime.Now;
            log.IP = HttpHelper.GetTrueUserIP().ToMaxLengthString(500);
            log.OSInfo = HttpHelper.GetUserOS().ToMaxLengthString(20);
            log.UserAgent= HttpContext.Current.Request.UserAgent.ToMaxLengthString(300);
            log.BrowserInfo = HttpContext.Current.Request.Browser.Browser.ToMaxLengthString(100);
            log.LoginChannel = (byte)EnumLib.SysLoginLog.LoginChannel.PC;//默认都是pc登录
            log.remark = remark;

            return DB_Base.InsertModel(log) > 0;
        }
    }
}
